<template>
  <div>
    <div class="tab-list flex-a">
      <div :class="tab == item.value ? 'active item' : 'item'" v-for="item in tabList" :key="item.value"
        @click="changeTab(item.value)">
        <span>{{ item.name }}</span>
        <i></i>
      </div>
    </div>
    <div class="box">
      <!-- 店员管理 -->
      <div v-if="tab == 'staff'">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" class="input">
          <el-form-item>
            <el-select v-model="queryParams.status" filterable placeholder="请选择店员状态" class="input" clearable>
              <el-option :label="item.name" :value="item.id + ''" v-for="item in stateList" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input v-model="queryParams.staffRole" placeholder="请输入店员职位" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="queryParams.staffName" prefix-icon="el-icon-search" placeholder="请输入店员姓名 / 店员账号"
              clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="btn" size="mini" @click="handleQuery">查询</el-button>
            <el-button type="primary" class="btn" size="mini" @click="handleAdd"
              v-hasPermi="['pet:ebstaff:add']">新增店员</el-button>
          </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="ebstaffList" class="grey">
          <el-table-column label="店铺号" align="center" prop="storeNo" />
          <el-table-column label="店员姓名" align="center" prop="staffName" />
          <el-table-column label="店员手机号" align="center" prop="staffPhone" />
          <el-table-column label="店员身份证号" align="center" prop="staffCode" />
          <el-table-column label="密码" align="center" prop="staffPwd" />
          <el-table-column label="店员职位" align="center" prop="staffRole" />
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              {{ scope.row.status == 1 ? '正常' : scope.row.status == 2 ? '离职' : scope.row.status == 3 ? '锁定' : '' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['pet:ebstaff:edit']">编辑</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['pet:ebstaff:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />
      </div>
      <!-- 职位管理 -->
      <div v-if="tab == 'job'">
        <ebrole></ebrole>
      </div>
    </div>
  </div>
</template>

<script>
import { listEbstaff, getEbstaff, delEbstaff, addEbstaff, updateEbstaff } from "@/api/pet/ebstaff";
import ebrole from "./../ebrole/index"
export default {
  name: "Ebstaff",
  components: {
    ebrole
  },
  data() {
    return {
      tab: 'staff',
      tabList: [
        {
          name: '店员管理',
          value: 'staff'
        },
        {
          name: '职位管理',
          value: 'job'
        },
      ],
      stateList: [
        {
          name: '正常',
          id: 1
        },
        {
          name: '离职',
          id: 2
        },
        {
          name: '锁定',
          id: 3
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工信息表格数据
      ebstaffList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeNo: null,
        staffName: null,
        staffPhone: null,
        staffCode: null,
        staffPwd: null,
        staffRole: null,
        status: null,
        createUser: null,
        updateUser: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询员工信息列表 */
    getList() {
      this.loading = true;
      listEbstaff(this.queryParams).then(response => {
        this.ebstaffList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // tab切换
    changeTab(value) {
      this.tab = value
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeNo: null,
        staffName: null,
        staffPhone: null,
        staffCode: null,
        staffPwd: null,
        staffRole: null,
        status: null,
        remark: null,
        createTime: null,
        createUser: null,
        updateTime: null,
        updateUser: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ path: '/store/addstaff', query: {} })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({ path: '/store/addstaff', query: {id:row.id} })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEbstaff(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEbstaff(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除员工信息编号为"' + ids + '"的数据项？').then(function () {
        return delEbstaff(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pet/ebstaff/export', {
        ...this.queryParams
      }, `ebstaff_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
